package com.caigp.cookbook2.activity;

import androidx.annotation.NonNull;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;

import com.caigp.cookbook2.R;
import com.caigp.cookbook2.base.BaseActivity;
import com.caigp.cookbook2.bean.CookBook;
import com.caigp.cookbook2.fragment.ListFragment;
import com.caigp.cookbook2.util.AppUtil;
import com.caigp.cookbook2.util.HttpClient;
import com.caigp.cookbook2.util.JsonUtil;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MenuListActivity extends BaseActivity {

    private static final String TAG = MenuListActivity.class.getSimpleName();

    private ListFragment listFragment;

    private int start = 0;

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    String result = (String) msg.obj;
                    JsonObject jsonObject = JsonParser.parseString(result).getAsJsonObject();
                    int status = jsonObject.get("status").getAsInt();
                    if (status == 0) {
                        JsonObject resultJsonObject = jsonObject.get("result").getAsJsonObject();
                        JsonArray jsonArray = resultJsonObject.getAsJsonArray("list");
                        List<CookBook> cookBookList = JsonUtil.getList(jsonArray.toString(), CookBook.class);
                        listFragment.setCookBookList(cookBookList);
                    } else if (status == 205) {
                        //没有信息
                        listFragment.setLoadEnd();
                    }
                    break;
                case 1:
                    start -= 10;
                    listFragment.setLoadMoreFail();
                    break;
            }
        }
    };

    private int classid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu_list);

        listFragment = (ListFragment) getSupportFragmentManager().findFragmentById(R.id.list_fragment);
        listFragment.setRecyclerViewListener(new ListFragment.RecyclerViewListener() {
            @Override
            public void scrollToTop() {

            }

            @Override
            public void scrollToBottom() {
                start += 10;
                requestData();
            }
        });
        Intent intent = getIntent();
        classid = intent.getIntExtra("classid", 0);
        String name = intent.getStringExtra("name");
        setActionBarTitle(name);
        requestData();
    }

    @Override
    protected boolean showHomeAsUp() {
        return true;
    }

    private void requestData() {
        RequestBody requestBody = new FormBody.Builder()
                .add("classid", String.valueOf(classid))
                .add("start", String.valueOf(start))
                .add("appkey", AppUtil.appkey)
                .build();
        HttpClient.sendPost(AppUtil.byClass, requestBody, new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                handler.sendEmptyMessage(1);
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                int code = response.code();
                if (code == 200) {
                    String body = response.body().string();
                    Message message = handler.obtainMessage();
                    message.obj = body;
                    message.what = 0;
                    handler.sendMessage(message);
                }
            }
        });
    }
}
